@Echo off

REM %1 - Entire path to wsp file
REM %2 - name of wsp file 
REM %3 - allowgacdeployment Y or N
REM %4 - Url - optional

REM Check for Empty string for wsp file name
IF X%1==X (
	Echo Solution File path cannot be empty
	Exit /b -1	
) 

IF NOT EXIST %1 (
	REM Check solution file path exists
	Echo Solution file path does not exists
	Exit /b -1	
)

IF X%2==X (
	REM Check for Empty string for path to wsp
	Echo Solution name cannot be empty
	Exit /b -1	
)

IF X%3==X (
	REM Check for Empty string for allowgacdeployment parameter
	Echo Fourth parameter allowgacdeployment cannot be empty should be Y or N
	Exit /b -1	
)
 
IF %3 NEQ Y (
	IF %3 NEQ N (
		Echo Fourth parameter should be Y or N
		Exit /b -1	
	) 
)

REM Change this path if it is differnet in your environment
@SET STSADM="%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\12\BIN\stsadm.exe"

REM Check for path exists for stsadm command
IF NOT EXIST %STSADM%  (
	Echo Stsadm path %STSADM% does not exists.
	Exit /b -1	
)

Echo Adding %1 (%2) solution to farm
%STSADM% -o addsolution -filename %1
%STSADM% -o execadmsvcjobs

Echo Deploying Solution %2 to farm to Url %4
IF %3==Y (
	IF X%4==X (
		Echo Deploying immediate, allowgac, force and no Url
		%STSADM% -o deploysolution -name %2 -immediate -allowGacDeployment -force		
	) ELSE (
		Echo Deploying immediate, allowgac, force and with Url
		%STSADM% -o deploysolution -name %2 -immediate -allowGacDeployment -force -url %4
	)
) ELSE (
	IF X%4==X (
		Echo Deploying immediate,no gac, force and no Url
		%STSADM% -o deploysolution -name %2 -immediate -force	
	) ELSE (
		Echo Deploying immediate, no gac, force and with Url
		%STSADM% -o deploysolution -name %2 -immediate -force -url %4
	)
)

Echo calling execadmsvc to start the timerjob to deploy the solution
%STSADM% -o execadmsvcjobs

REM this is to copy the code behind assembly _layouts pages to appbin folder under each web application
%STSADM% -o copyappbincontent

Goto End

:End